
//
// 120327 - (AH) - (Midtun) Created.
// 120329 - (AH) - (Midtun *) Parameter on folder.
// 120401a- (AH) - Extends ParameterBase.
// 120401b. (AH) - Owner property.
//

package org.soekrat.server.io.db;

import com.googlecode.objectify.annotation.Subclass;

/**
 * <p>
 *   Represents a folder in the data base where 
 *   {@link DbFile}s and sub folders are stored.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@Subclass
public final class Folder extends ParameterBase 
{
  private String name="unnamed";
  
  private Long ownerId;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public Folder(){}
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * <p>
   *   Folder name will be "unnamed"-
   * </p> 
   * @param ownerId Id of folder that {@code this}, {@code -1} if this is top
   *                level.
   */
  public Folder(long ownerId)
  {
    if (ownerId<-1)
    {
      throw new IllegalArgumentException("ownerId<-1 : "+ownerId);
    }
    
    this.ownerId=ownerId;
  }
  
  /**
   * <p>
   *   Constructor for top level.
   * </p>
   * @param name Name on folder.
   * @throws IllegalArgumentException If {@code name} is the empty string.
   */
  public Folder(String name){ name(name);  ownerId=-1L; }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param name Name on folder.
   * @param ownerId Id of folder that {@code this}, {@code -1} if this is top
   *                level. 
   * @throws IllegalArgumentException If {@code name} is the empty string.
   */
  public Folder(String name, long ownerId)
  { 
    if (ownerId<-1)
    {
      throw new IllegalArgumentException("ownerId<-1 : "+ownerId);
    }
    
    this.ownerId=ownerId;
    
    name(name); 
  }
  
  /**
   * <p>
   *   Sets the name of the folder.
   * </p>
   * @param name Name on folder.
   * @return {@code this}.
   */
  public Folder name(String name)
  {
    if (name==null)
    {
      throw new NullPointerException("name");
    }
    
    name=name.trim();
    if (name.length()==0)
    {
      throw new IllegalArgumentException("name can not be the empty string");
    }
    
    this.name=name;
    
    return this;
  }
  
  /**
   * <p>
   *   Gets id of the folder this is in.
   * </p>
   * @return Id or {@code -1} if not sat (will always be the case if is top
   *         level folder).
   */
  public long ownerId(){ return this.ownerId==null ? -1 : this.ownerId; } 
  
  /**
   * <p>
   *   Gets folder name.
   * </p>
   * @return Name.
   */
  public String name(){ return this.name; }
  
  /**
   * <p>
   *   The empty array shared.
   * </p>
   */
  public static final Folder[] EMPTY_ARRAY=new Folder[0];
  
}
